﻿/**************************************************************
*作者：Leon
*创建时间：2022/4/5 16:13:41
**************************************************************/
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Input;
using Microsoft.Toolkit.Mvvm.Messaging.Messages;
using System.Windows;
using System.Windows.Media;

namespace Lad.DrawProject.Controls;

public interface ISelectable
{
    bool IsSelected { get; set; }
}
public interface IGroupable
{
    Guid Id { get; }
    Guid ParentId { get; set; }
}
/// <summary>
/// 基础图形设计
/// </summary>
public abstract class SelectableItemViewModelBase : ObservableRecipientAction, ISelectable, IGroupable
{
    public Guid Id { get; set; }

    public Guid ParentId { get ; set; }
   
    private bool _IsSelected;

    public bool IsSelected
    {
        get { return _IsSelected; }
        set {
            if (SetProperty(ref _IsSelected, value))
            {
                //如果没有文字，失去焦点自动清除
                if (!_IsSelected && string.IsNullOrEmpty(Text))
                {
                    ShowText = false;
                    //if (this is TextDesignerItemViewModel)
                    //{
                    //    if (ParentItem != null)
                    //    {
                    //        ParentItem.OutTextItem = null;
                    //    }
                    //    Parent.DirectRemoveItemCommand.Execute(this);
                    //}
                }

            }
        }
    }

    private double _Top;

    public virtual double Top
    {
        get { return _Top; }
        set { SetProperty(ref _Top, value); }
    }
    private  double _Left;

    public virtual double Left
    {
        get { return _Left; }
        set { SetProperty(ref _Left, value); }
    }
    private int _ZIndex;

    public int ZIndex
    {
        get { return _ZIndex; }
        set { SetProperty(ref _ZIndex, value); }
    }
    private double _Width;

    public virtual double Width
    {
        get { return _Width; }
        set { SetProperty(ref _Width, value); }
    }
    private double _Height;

    public virtual double Height
    {
        get { return _Height; }
        set { SetProperty(ref _Height, value); }
    }
    private bool _IsHitTestVisible;

    public bool IsHitTestVisible
    {
        get { return _IsHitTestVisible; }
        set { SetProperty(ref _IsHitTestVisible, value); }
    }

    private bool _IsReadOnlyText;

    public bool IsReadOnlyText
    {
        get { return _IsReadOnlyText; }
        set { SetProperty(ref _IsReadOnlyText, value); }
    }
    private bool _ShowText;

    public virtual bool ShowText
    {
        get { return _ShowText; }
        set { SetProperty(ref _ShowText, value); }
    }
    private double _ScaleX;

    public double ScaleX
    {
        get { return _ScaleX; }
        set { SetProperty(ref _ScaleX, value); }
    }
    private double _ScaleY;

    public double ScaleY
    {
        get { return _ScaleY; }
        set { SetProperty(ref _ScaleY, value); }
    }
    private double _Angle;

    public double Angle
    {
        get { return _Angle; }
        set { SetProperty(ref _Angle, value); }
    }

    private Color? _ShadowColor;

    public Color? ShadowColor
    {
        get { return _ShadowColor; }
        set { SetProperty(ref _ShadowColor, value); }
    }
    private bool _IsReadOnly;

    public bool IsReadOnly
    {
        get { return _IsReadOnly; }
        set { SetProperty(ref _IsReadOnly, value); }
    }

    private string? _Text;

    public string? Text
    {
        get { return _Text; }
        set {
            if (SetProperty(ref _Text, value))
            {
                if (!string.IsNullOrEmpty(_Text))
                    ShowText = true;
            }
        }
    }

    #region 颜色

    private IColorViewModel _ColorViewModel;

    public IColorViewModel ColorViewModel
    {
        get { return _ColorViewModel; }
        set { SetProperty(ref _ColorViewModel, value); }
    }
    private IFontViewModel? _FontViewModel;

    public IFontViewModel? FontViewModel
    {
        get { return _FontViewModel; }
        set { SetProperty(ref _FontViewModel, value); }
    }

    #endregion


    /// <summary>
    /// 文字对象
    /// </summary>
    //public SelectableItemViewModelBase? OutTextItem { get; set; }

    private IRelayCommand? _EditTextCommand;

    public  IRelayCommand EditTextCommand
    {
        get
        {
            return _EditTextCommand ??= new RelayCommand(ExcuteEditTextCommand);
        }
    }

    protected virtual void ExcuteEditTextCommand()
    {
        if (IsReadOnly == true)
            return;
        ShowText = true;
    }
    public SelectableItemViewModelBase()
    {
        _ColorViewModel = Service.CopyDefaultColorViewModel();
        _FontViewModel = Service.CopyDefaultFontViewModel();
        Init();
    }
    protected virtual void Init()
    {
        _ScaleX = _ScaleY = 1;
    }
    /// <summary>
    /// 初始化数据
    /// </summary>
    /// <returns></returns>
    public virtual bool InitData()
    {
        return true;
    }
    /// <summary>
    /// 编辑数据
    /// </summary>
    /// <returns></returns>
    public virtual bool EditData()
    {
        return true;
    }
}

/// <summary>
/// 基础画布图形实体
/// </summary>
public abstract class DesignerItemViewModelBase : SelectableItemViewModelBase
{
    public DesignerItemViewModelBase() : base()
    {
    }
    private string? _Icon;

    public string? Icon
    {
        get { return _Icon; }
        set { SetProperty(ref _Icon, value); }
    }


    public void RaiseTopLeft()
    {
       // OnPropertyChanged(nameof(TopLeft));
        
        //AddAction(this, new Point(GetOldValue<double>(nameof(Left)), GetOldValue<double>(nameof(Top))),
        //    TopLeft, nameof(TopLeft));
        AddActions(new ActionGroup("RaiseTopLeft Action", new() {
            new PropertyChangedAction<double>(this, GetOldValue<double>(nameof(Left)), Left, nameof(Left)),
            new PropertyChangedAction<double>(this, GetOldValue<double>(nameof(Top)), Top, nameof(Top))
        }));
    }

    public void RaiseItemWidthHeight()
    {
        //OnPropertyChanged(nameof(ItemWidthHeight));

        //AddAction(this, new Point(GetOldValue<double>(nameof(Width)), GetOldValue<double>(nameof(Height))),
        //    ItemWidthHeight, nameof(ItemWidthHeight));

        AddActions(new ActionGroup("RaiseItemWidthHeight Action", new() {
            new PropertyChangedAction<double>(this, GetOldValue<double>(nameof(Width)), Width, nameof(Width)),
            new PropertyChangedAction<double>(this, GetOldValue<double>(nameof(Height)), Height, nameof(Height))
        }));

    }
    public void RaiseAngle()
    {
        //OnPropertyChanged(nameof(Angle));
        AddAction(this, GetOldValue<double>(nameof(Angle)),
            Angle, nameof(Angle));
    }
}

/// <summary>
/// 文字设计
/// </summary>
public class TextDesignerItemViewModel : DesignerItemViewModelBase
{

    public TextDesignerItemViewModel()
    {

    }

    protected override void Init()
    {
        base.Init();

        this.Width = 150;
    }

    protected override void ExcuteEditTextCommand()
    {
        if (IsReadOnly == true)
            return;
    }


    private string _Watermark = "请输入文本";

    public string Watermark
    {
        get { return _Watermark; }
        set { SetProperty(ref _Watermark, value); }
    }


    //固定在DataTemplate中处理
    private bool _ShowText;

    public override bool ShowText
    {
        get { return _ShowText; }
        set { SetProperty(ref _ShowText, value); }
    }


}
